﻿using System;
using System.Data;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace LightWeb.Infrastructure.Repositories
{
    public partial class DataContext
    {
        private static string _contextStoreKey = "CONTEXT_STORE_KEY";
        public DataContext()
            : this(SessionFactory.GetSessionInstance())
        {

        }

        public DataContext(string configFileName)
            : this(SessionFactory.GetSessionInstance(configFileName))
        {

        }

        public DataContext(ISession session)
        {
            _session = session;
            Current = this;
        }

        public static void Init()
        {
            Init(string.Empty);
        }

        public static void Init(string fileName)
        {
            SessionFactory.Initial(fileName);
        }

        public static void ForceInit(string fileName = "")
        {
            SessionFactory.ForceInitial(fileName);
        }

        public static void ReCreateDatabase()
        {
            var _cfg = new Configuration();
            _cfg.Configure();
            SchemaExport export = new SchemaExport(_cfg);
            export.Execute(true, true, false);
        }

        private ISession _session;

        public ISession Session
        {
            get
            {
                ReConnectIfRequired();
                return _session;
            }
            set { _session = value; }
        }

        public static DataContext Current
        {
            get
            {
                DataContext value = ContextStorage.Load<DataContext>(_contextStoreKey);
                if (value == null)
                {
                    value = new DataContext();
                }
                return value;
            }
            private set
            {
                if (value != null)
                {
                    ContextStorage.Save(_contextStoreKey, value);
                }
            }
        }

        public void ReConnectIfRequired()
        {
            if (_session.Connection.State != ConnectionState.Open)
            {
                _session.Connection.Open();
            }
            if (!_session.IsConnected)
            {
                _session.Reconnect(_session.Connection);
            }
        }

        internal ITransaction BeginTransaction()
        {
            if (Session == null)
            {
                throw new Exception("Session为空");
            }
            return Session.BeginTransaction();
        }

        internal void Commit()
        {
            if (Session == null)
            {
                throw new Exception("Session为空");
            }
            Session.Transaction.Commit();
        }

        internal void RollBack()
        {
            if (Session == null)
            {
                throw new Exception("Session为空");
            }
            Session.Transaction.Rollback();
        }
    }
}
